<?php
//模块名_控制名
class Warehouse_OnePieceController extends Zend_Controller_Action
{
	private $_s;
	public function preDispatch(){
		$this->_s=new Order_Service_Order();
		$this->tplDirectory = "warehouse/onepiece/";	//控制对应模板目录
	}
	
	public function indexAction(){
		echo Cff::renderTpl($this->tplDirectory."index.tpl","layout");
		
    }
    
    public function pickupInfoAction(){
    	$lang = Zend_Registry::get('lang');
    	$op_code = $this->_request->getParam('op_code','');
    	$data = array ('ask'=>0);
    	$cond = array();
    	$cond['op_code'] = $op_code;
    	$cond['op_status'] = 0;
//     	print_r($_COOKIE);
//     	print_r($lang);
    	$pickupInfo = Order_Service_OrderPickup::getByCond($cond);
    	if(empty($pickupInfo)){
    		$data['msg'] = array($lang['op_has_deal']);
    		die (Zend_Json::encode($data));	 
    	}
    	
    	$pickupRows = Order_Service_OrderPickupMap::getByfield('op_code',$op_code);
    	if(empty($pickupRows )){
    		$data['msg'] = array($lang['make_sure_op_code_is_right']);
    		die (Zend_Json::encode($data));	 
    	}
    	$total = count($pickupRows);
    	$scanQuantity = 0;				//已扫描数量
    	if($total > 0){
    		foreach ($pickupRows as $key => $pickupRow){
				$orderInfo = Order_Service_Order::getByOrdersCode($pickupRow['orders_code']);
				if($orderInfo['process_status'] == 4){
					$scanQuantity++;
				}
				$orderProductRows = Order_Service_OrderProduct::getAllOrderCodeByValue($pickupRow['orders_code'],'orders_code');
				if(count($orderProductRows) > 1){
					$data['msg'] = array($lang['op_code_not_one_piece']);
    				die (Zend_Json::encode($data));
				}elseif (count($orderProductRows)==1 && $orderProductRows[0]['op_quantity'] > 1){
					$data['msg'] = array($lang['one_order_not_one_piece']);
    				die (Zend_Json::encode($data));
				}				
			}
			$data['ask'] = 1;
			$data['noscan'] = $total - $scanQuantity;		//未扫描数量
			$data['sacned'] = $scanQuantity;
			$data['op_code'] = $op_code;
// 			print_r($pickupInfo);exit;
			$data['last_pack_orders_code'] = $pickupInfo[0]['last_pack_orders_code'];
			
    	}
    	die (Zend_Json::encode($data));	  	
    	
    }
    
    public function productInfoAction()
    {    	
    	$op_code = $this->_request->getParam('op_code','');
    	$productStr = $this->_request->getParam('productStr','');
    	
    	$data = array ('ask'=>0);
    	if(empty($productStr)){
    		$data['msg'] = array($this->lang['make_sure_barcode_is_right']);
    		die (Zend_Json::encode($data));	 
    	}
    	
    	$cond = array();
    	$cond['onepiece_sku'] = $productStr;  	
    	$productRow = Product_Service_Product::getRowByCond($cond);		//查找指定产品信息
    	
    	if(empty($productRow)){
    		$data['msg'] = array($this->lang['barcode_err']);
    		die (Zend_Json::encode($data));
    	}
    	
    	$cond = array();
    	$cond['op_code'] = $op_code;
    	$cond['product_id'] = $productRow['product_id'];
    	$orderby = array('opm_id desc');
    	$pickupRows = Order_Service_OrderPickupMap::getAllByCond($cond,'*',$orderby);

    	// 订单处理		//产品ID确定，订单随机 	
    	$flag = false;
    	$data['msg'] = array($this->lang['op_code_do_not_contain_this_barcode']);
    	foreach ($pickupRows as $key => $pickupRow){
    		$orderInfo = Order_Service_Order::getByOrdersCode($pickupRow['orders_code']);
    		$productRow['shipping_method'] = $orderInfo['shipping_method'];					//产品的运输方式 //$_SERVER['HTTP_USER_AGENT']
    		
    		if(preg_match('/IE/i',$_SERVER['HTTP_USER_AGENT']) && $orderInfo['shipping_method'] != 'EPACKET'){
    			$data['msg'] = array($this->lang['use_firefox_not_support_ie']);
    			die (Zend_Json::encode($data));
    		}
    		
			if($orderInfo['process_status'] == 2){
				$orderCodeCurrent = $pickupRow['orders_code'];
				$wpbId = $pickupRow['wpb_id'];					//批次记录
				
				//处理订单状态更新、库存更新
				$data  = Warehouse_Service_OnepieceProccess::onepieceUpdate($productRow['product_id'],$orderCodeCurrent,$wpbId);										
				if(!empty($data['ask'])){					
					$flag = true;
				}
			}
			if($flag){
				break;		//一票一件 只执行一次
			}
    	}
    	
    	$data['pp_name'] = '无';
    	$data['product'] = '';
    	$productRow['pp_name'] = '';	//产品对应的包材名称    	
    	if($flag){
    		
    		//获取物流单号	//测试时用
//			$orderShipInfo = Order_Service_OrderShip::getLastRowByOrdersCode ($orderCodeCurrent);
//			$data['tracking_number'] = $orderShipInfo['tracking_number'];
//			$data['orders_code'] = $orderCodeCurrent;
//			$data['ask'] = 1;
			
    		$data['product'] = $productRow;    		
    		$product_PackageMap = Product_Service_PackageMap::getAllByProductId ($productRow['product_id']);	//返回产品与包材对应信息
    		if(!empty($product_PackageMap)){
    			$packageInfo = Product_Service_Package::getByPpid ($product_PackageMap['package_id']);
    			$productRow['pp_name'] = $packageInfo['pp_name'];
				$packageInfo['pp_name'] && $data['pp_name'] = $packageInfo['pp_name'];
				
    		}
    	}
    	
    	die (Zend_Json::encode($data));	 
    	
    }
    
    public function scanConfirmAction()
    {
    	$op_code = $this->_request->getParam('op_code','');
    	$data = array ('ask'=>0);    	
    	$data = Warehouse_Service_OnepieceProccess::scanConfirm($op_code);
    	
    	if(!empty($data['op_code'])){	// 存在新下架单
    		$sessionPickup = new Zend_Session_Namespace('newpickup');
    		$sessionPickup->pickup = $data;
    		unset($data['order']);
    	}else {
    		$data['ask'] = 1;
    		$data['msg'] = array($op_code.$this->lang['all_product_scan_finish']);
    	}
    	
    	die (Zend_Json::encode($data));	
    	
    }
    
    public function printNewPickupAction(){
    	$sessionPickup = new Zend_Session_Namespace('newpickup');
    	$data = $sessionPickup->pickup;
    	$sessionPickup->unsetAll();
//    	print_r($data);exit();
    	$this->view->data = $data;
    	echo $this->view->render($this->tplDirectory."print_new.tpl");
    }
    
	public function printPopAction(){
		$code=trim($this->_request->getParam('orders_code',''));	
		$reprint = 	$this->_request->getParam('reprint','');
		$order = $this->_s->getByfield('orders_code', $code);
		$order = $order[0];
		
		$orm = Order_Service_OrderPickupMap::getByfield('orders_code', $code);
		$orm = $orm[0];
		$order['op_code'] = $orm['op_code'];
// 		print_r($orm);exit;
		$product = Order_Service_Product::getByfield('orders_code', $code);
		$product['pp_name'] = '';
		if($product){
		    $product = $product[0];
		    $product_PackageMap = Product_Service_PackageMap::getAllByProductId ($product['product_id']);	//返回产品与包材对应信息
		    if(!empty($product_PackageMap)){
		        $packageInfo = Product_Service_Package::getByPpid ($product_PackageMap['package_id']);
		        $product['pp_name'] = $packageInfo['pp_name'];
		    
		    }
		}		
		$category = Product_Service_Product::getProductCategory($product['product_id']);
		if($category){
		    $product['poc_value_shortname'] = $category['poc_value_shortname'];
		}
// 		print_r($order);
// 		print_r($product);
// 		print_r($category);
		$this->view->order = $order;
		$this->view->product = $product;
		$this->view->reprint = $reprint;
		echo $this->view->render($this->tplDirectory.'print-detail3.tpl');exit;
	}
	
    
    
}